//
//  RedPacketRecordVC.m
//  RPay
//
//  Created by hzy on 2017/10/31.
//  Copyright © 2017年 Charlie. All rights reserved.
//

#import "RedPacketRecordVC.h"
#import "RedPacketRecordCell.h"
#import "TMNetwork.h"
#import "RecordModel.h"
#import "OpenRedPacketVC.h"
#import "RefreshManager.h"
#import "HexColors.h"

@interface RedPacketRecordVC ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentControl;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSMutableArray * dataSource;
@property (nonatomic,strong)NSMutableArray * getDataSource;
@property (nonatomic,strong)NSMutableArray * sendDataSource;
@property (nonatomic,assign)NSInteger getIndex;
@property (nonatomic,assign)NSInteger sendIndex;
@property (nonatomic,strong)RefreshManager * refreshManager;
@property (nonatomic,strong)MBProgressHUD * hud;


@end

@implementation RedPacketRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"红包记录";
    [self addNavigationBarLeftButtonItem];
    [self setupUI];


    
    
}
-(void)setupUI{
    _dataSource = [NSMutableArray array];
    _getDataSource = [NSMutableArray array];
    _sendDataSource = [NSMutableArray array];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self initRefremanager];
    


}
- (IBAction)segmentValueChange:(UISegmentedControl *)sender {
    
    [_refreshManager.headerView endRefreshing];
    [_refreshManager.footerView endRefreshing];
    
    
    if (sender.selectedSegmentIndex == 0) {
        [_refreshManager.formParams setObject:@"get" forKey:@"type"];
        if (_getDataSource.count) {
            _dataSource = _getDataSource;
            [_tableView reloadData];
        }else{
            _hud =  [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            _dataSource = _getDataSource;
            [_tableView reloadData];
            [_refreshManager.headerView beginRefreshing];
        }
        
    }else if (sender.selectedSegmentIndex == 1){
    [_refreshManager.formParams setObject:@"send" forKey:@"type"];
        if (_sendDataSource.count) {
            _dataSource = _sendDataSource;
            [_tableView reloadData];
            
        }else{
           _hud =  [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            _dataSource = _sendDataSource;
            [_tableView reloadData];
            [_refreshManager.headerView beginRefreshing];
        }
        
    }
    
    
}

-(void)initRefremanager{
    MJWeakSelf

    
    

    _refreshManager = [[RefreshManager alloc] initWithurl:@"/open/appRedPacket/getRedPacket.shtml?" Params:@{@"userName":[ShareUser shareUser].userName,@"type":@"get"}];
    
    _refreshManager.headerCompleteBlock = ^(id responseObject) {
        NSLog(@"%@",responseObject);
        NSArray * array = nil;
        if (_segmentControl.selectedSegmentIndex == 0) {
             [weakSelf.getDataSource removeAllObjects];
             array  = [RecordModel mj_objectArrayWithKeyValuesArray:responseObject[@"page"]];
            

        }else{
        
            
             [weakSelf.sendDataSource removeAllObjects];
            array  = [RecordModel mj_objectArrayWithKeyValuesArray:responseObject[@"page"]];
        }
       
        [weakSelf refreshData:array];
        
        
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        
    };
    _refreshManager.beginCompleteBlock = ^{
      
        if (!weakSelf.hud) {
            [MBProgressHUD showHUDAddedTo:weakSelf.view animated:YES];
        }
        
    };
    _refreshManager.footerCompleteBlock = ^(id responseObject) {
        NSArray * array = nil;
        if (_segmentControl.selectedSegmentIndex == 0) {

            array  = [RecordModel mj_objectArrayWithKeyValuesArray:responseObject[@"page"]];
            

        }else{
            
            

            array  = [RecordModel mj_objectArrayWithKeyValuesArray:responseObject[@"page"]];
        }
        
        [weakSelf refreshData:array];

        

        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
    };
    _refreshManager.failedSuccessBlock = ^{
        if (weakSelf.hud) {
            [weakSelf.hud hideAnimated:YES];
            _hud = nil;
        }
        
         [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
    };
    [_refreshManager setRefreshHeader:_tableView];
    [_refreshManager setRefreshfooter:_tableView];
    
    [_refreshManager.headerView beginRefreshing];

}
-(void)refreshData:(NSArray *)array{
    
    if (_segmentControl.selectedSegmentIndex == 0) {
        
        
        [_getDataSource addObjectsFromArray:array];
        _dataSource = _getDataSource;
    }else{
        
        
        
        
        [_sendDataSource addObjectsFromArray:array];
        _dataSource = _sendDataSource;
    }
    
    [_tableView reloadData];
    

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return  _dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 55;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static  NSString  *CellIdentiferId = @"RedPacketRecordCell";
    RedPacketRecordCell  *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentiferId];
    if (cell == nil) {
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"RedPacketRecordCell" owner:nil options:nil];
        cell = [nibs lastObject];
        cell.backgroundColor = [UIColor clearColor];
    };
    RecordModel * model = _dataSource[indexPath.row];
    NSString * string = nil;
    if (_segmentControl.selectedSegmentIndex == 0) {
        string =[NSString stringWithFormat:@"来自 %@ 的红包",model.sendUser];
        cell.moneyLab.text = [NSString stringWithFormat:@"+ %.2f",[model.takeInfo[@"takeInMoney"] doubleValue]];
        cell.moneyLab.textColor = [UIColor hx_colorWithHexRGBAString:@"#D3A05A"];
    }else{
        
        if (model.packetCount > 1) {
            string = @"群红包";
        }else{
        
            string = [NSString stringWithFormat:@"发送给%@的红包",[model.takeInUser stringByReplacingOccurrencesOfString:@"," withString:@""]];
        }
        
        cell.moneyLab.text = [NSString stringWithFormat:@"- %.2f",[model.money doubleValue]];
        cell.moneyLab.textColor = [UIColor hx_colorWithHexRGBAString:@"#24ac69"];
    }
    cell.fromLab.text = string;
    
    cell.timeLab.text = model.sendTime;

    return cell;
}
-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [_tableView deselectRowAtIndexPath:indexPath animated:YES];

    OpenRedPacketVC * vc = [[OpenRedPacketVC alloc] initWithNibName:@"OpenRedPacketVC" bundle:[NSBundle mainBundle]];
    RecordModel * model = _dataSource[indexPath.row];
    
    vc.isMine = _segmentControl.selectedSegmentIndex ==1;
    
    if (_segmentControl.selectedSegmentIndex == 0) {
        vc.aid = model.takeInfo[@"id"];

    }else{
    
        vc.aid = model.aid;
    }
    [self.navigationController pushViewController:vc animated:YES];
    
    
    
}


@end
